﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.MODEL;
using KPIS.GERP.ESS.APP.App_UI.Commons;
using Telerik.Web.UI;
using KPIS.GERP.ESS.APP.App_UI.Forms;

namespace KPIS.GERP.ESS.APP.App_UI.UserControls
{
    public partial class Mail_FimMoneyReturns : System.Web.UI.UserControl
    {
        Main main = new Main();
        ESS.BLL.B_MessageSend send = new ESS.BLL.B_MessageSend();
        FIM_MoneyRequests.FIM_MoneyRequests mr = new FIM_MoneyRequests.FIM_MoneyRequests();
        FIM_MoneyReturns.FIM_MoneyReturns re = new FIM_MoneyReturns.FIM_MoneyReturns();
        FIM_MoneyReturnDtls.FIM_MoneyReturnDtls red = new FIM_MoneyReturnDtls.FIM_MoneyReturnDtls();

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void LoadData(string strSendSeq, string strReturnSeq)
        {
            lblSendSeq.Text = strSendSeq;
            M_MessageSend info = send.GetInfo(strSendSeq);
            FIM_MoneyReturns.M_FIM_MoneyReturns reInfo = re.GetInfo(strReturnSeq);
            FIM_MoneyRequests.M_FIM_MoneyRequests mrInfo = mr.GetInfo(reInfo.money_req_seq.ToString());
            lblMoneyReqSeq.Text = mrInfo.money_req_seq.ToString();
            lblMoneyReqName.Text = mrInfo.money_req_name;
            lblBorrowNo.Text = mrInfo.borrow_no;
            lblBorrowReturnDate.Text = (mrInfo.borrow_return_date != null ? mrInfo.borrow_return_date.Value.ToShortDateString() : "-");
            lblRemarks.Text = mrInfo.remarks;
            lblBgmProjOperSeq.Text = mrInfo.bgm_proj_oper_seq.ToString();
            lblBgmExpGrpSeq.Text = mrInfo.bgm_exp_grp_seq.ToString();

            lblReturnSeq.Text = strReturnSeq;
            lblBgmSourceSeq.Text = (mrInfo.bgm_source_seq ?? 1).ToString();
            LoadGrdMoneyReturnDtls();

            lblReturnDate.Text = reInfo.money_req_date.HasValue ? reInfo.money_req_date.Value.ToShortDateString() : "";
            lblReturnRefNo.Text = reInfo.ref_no;
        }

        protected void LoadGrdMoneyReturnDtls()
        {
            string strReturnSeq = lblReturnSeq.Text;

            var data = red.GetMoneyReqDtls(strReturnSeq, lblMoneyReqSeq.Text);

            grdMoneyReturnDtls.DataSource = data;
            grdMoneyReturnDtls.DataBind();

            if (lblReturnSeq.Text.Length > 0)
            {
                FIM_MoneyReturns.M_FIM_MoneyReturns info = re.GetInfo(lblReturnSeq.Text);
                lblSumReturnBathAmount.Text = (info.total_bath_amount_accum.Value).ToString("#,##0.00");
                lblSumBorrowBathAmount.Text = (info.money_req_total_bath_amount.Value).ToString("#,##0.00");
                lblSumReceiveBathAmount.Text = (info.receive_bath_amount.Value).ToString("#,##0.00");
                lblRemainAmount.Text = ((info.money_req_total_bath_amount ?? 0) - (info.total_bath_amount_accum ?? 0) - (info.receive_bath_amount ?? 0)).ToString("#,###.00");
            }
            else
            {
                FIM_MoneyRequests.M_FIM_MoneyRequests info = mr.GetInfo(lblMoneyReqSeq.Text);
                lblSumReturnBathAmount.Text = (info.return_amount ?? 0).ToString("#,##0.00");
                lblSumBorrowBathAmount.Text = (info.total_amount ?? 0).ToString("#,##0.00");
                lblSumReceiveBathAmount.Text = (info.receive_amount ?? 0).ToString("#,##0.00");
                lblRemainAmount.Text = ((info.total_amount ?? 0) - (info.return_amount ?? 0) - (info.receive_amount ?? 0)).ToString("#,###.00");
            }
        }

        protected void grdMoneyReturnDtls_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGrdMoneyReturnDtls();
            }
        }

        protected void cmdRead_Click(object sender, EventArgs e)
        {
            var model = send.GetInfo(lblSendSeq.Text);
            model.message_status = "3";
            model.updated_by = SsMng.EmpSeqI;
            model.updated_when = DateTime.Now;
            send.UpdateMessageStatus(model);
            main.MessageAlert(Page, "บันทึกเรียบร้อย");

            ((TaskListsPopUp)Page).cmdViewRequest_Click(null, null);
        }
    }
}